package server;

import java.beans.XMLDecoder;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

import common.ServerInterface;

public class RemoteShutdown {

	public static void main(String[] args){
		if(args.length<=0) printUsage();
		File serverConf = new File(args[0]);
		try{
			XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(new FileInputStream(serverConf)));
			ServerConfiguration config = (ServerConfiguration) decoder.readObject();
			decoder.close();
			ServerInterface server = (ServerInterface) Naming.lookup("rmi://"+config.getServerPublicHostname()+":"
					+ config.getServerPort() + "/" + config.getServerServiceName());
			server.shutDown(config.getKey());
		} catch (FileNotFoundException e){
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (NotBoundException e) {
			e.printStackTrace();
		}
		
	}

	private static void printUsage() {
		System.out.println("Usage:");
		System.out.println("RemoteShutdown <server xml>");
		System.exit(0);
	}
}
